﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Dao;
using System.Text.RegularExpressions;

namespace Web.Page
{
    public partial class CPBZX_Add : BasePage 
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.lblTitle.Text = "新增产品类型";
            }
        }

        protected void btnReturn_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("~/Page/ZL_CPBZX_Query.aspx");
        }

        protected void btnClear_Click(object sender, ImageClickEventArgs e)
        {
            ClearControlsValue();
        }

        private void ClearControlsValue()
        {
            this.tbbh.Text = "";
            this.tbbz.Text = "";
            this.tbmc.Text = "";
            this.tbnchang.Text = "";
            this.tbnhou.Text = "";
            this.tbnkuan.Text = "";
            this.tbsl.Text = "";
            this.tbtj.Text = "";
            this.tbwchang.Text = "";
            this.tbwhou.Text = "";
            this.tbwkuan.Text = "";
            this.tbzl.Text = "";
            this.tbbzfs.Text = "";
        }

        protected void btnSave0_Click(object sender, ImageClickEventArgs e)
        {
            this.lbResult.Text = "";
            int rbtb = 0;
            string resultMessage = "";
            if (this.rbcpbh1.Checked == true)
            {
                rbtb = 0;
            }
            else
            {
                rbtb = 1;
            }
            //string str = "^[0-9]+(.[0-9])?$";
            string str=@"^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$";
            Regex myRegex = new Regex(str);

            string str2 = "^([0-9]*)$";
            Regex myRegex2= new Regex(str2);
            if (!myRegex.IsMatch(tbwchang.Text.Trim()))
            {
                this.lbResult.Text = "请输入正确的外空-长";
                return;
            }
            if (!myRegex.IsMatch(tbwkuan.Text.Trim()))
            {
                this.lbResult.Text = "请输入正确的外空-宽";
                return;
            }
            if (!myRegex.IsMatch(tbwhou.Text.Trim()))
            {
                this.lbResult.Text = "请输入正确的外空-厚";
                return;
            }
            
            if (!myRegex.IsMatch(tbnchang.Text.Trim()))
            {
                this.lbResult.Text = "请输入正确的内空-长";
                return;
            }
            if (!myRegex.IsMatch(tbnkuan.Text.Trim()))
            {
                this.lbResult.Text = "请输入正确的内空-宽";
                return;

            }
            if (!myRegex.IsMatch(tbnhou.Text.Trim()))
            {
                this.lbResult.Text = "请输入正确的内空-厚";
                return;
            }
            
            if (!myRegex.IsMatch(tbtj.Text.Trim()))
            {
                this.lbResult.Text = "请输入正确的体积";
                return;
            }
            if (!myRegex.IsMatch(tbzl.Text.Trim()))
            {
                this.lbResult.Text = "请输入正确的重量";
                return;
            }
            if (!myRegex2.IsMatch(tbsl.Text.Trim()))
            {
                this.lbResult.Text = "请输入正确的数量";
                return;
            }         
            ZL_CPBZX entity = new ZL_CPBZX()
            {
                ID=Guid.NewGuid(),
                BH = this.tbbh .Text.Trim(),
                MC = this.tbmc .Text.Trim(),
                WCHANG=Convert.ToDouble ( this.tbwchang.Text.Trim()),
                WGAO =Convert.ToDouble ( this.tbwhou.Text.Trim ()),
                WKUAN =Convert.ToDouble (this.tbwkuan .Text.Trim ()),
                NCHANG =Convert.ToDouble (this.tbnchang .Text.Trim ()),
                NGAO =Convert.ToDouble (this.tbnhou .Text.Trim ()),
                NKUAN =Convert.ToDouble (this.tbnkuan .Text.Trim ()),
                TJ =Convert.ToDouble (this.tbtj.Text.Trim ()),
                ZL =Convert.ToDouble (this.tbzl.Text.Trim ()),
                BZFS=this.tbbzfs.Text.Trim (),
                SL=Convert.ToDouble ( this.tbsl.Text.Trim ()),                 
                BZ=this.tbbz.Text.Trim(),                 
                BZXLX =rbtb     
            };

            
            try
            {
                ZL_CPBZX_Dao  dao = new ZL_CPBZX_Dao ();
                int result = dao.Add(entity);

                if (result == 1)
                {
                    resultMessage = "保存成功";
                    ClearControlsValue();
                }
            }
            catch (Exception ex)
            {
                resultMessage = "保存失败" + ex.Message;
            }
            finally
            {
                this.lbResult.Text = resultMessage;
            }
        }

        protected void tbbh_TextChanged(object sender, EventArgs e)
        {
            lbResult.Text = "";
        }
    }
}